#!/bin/bash

# 设备目录
HOME_DIR=/root/home
DEPLOY_DIR=$HOME_DIR/deploy
SERVER_DIR=$HOME_DIR/servers
APP_NAME=aipi-start.jar

cd $DEPLOY_DIR/aipi-server

git checkout develop
if [ $? -ne 0 ]; then
  exit 4
fi

git pull -r
echo "git拉取最新代码..."
if [ $? -ne 0 ]; then
  exit 3
fi

mvn clean package -T4C -U
echo "正在构建服务Jar..."
if [ $? -ne 0 ]; then
  exit 2
fi

# 关闭服务
sh $SERVER_DIR/shutdown-aipi.sh

# 删除jar文件
if [ -e "$SERVER_DIR/apps/$APP_NAME" ]; then
  rm -rf $SERVER_DIR/apps/$APP_NAME
  echo "删除文件 $APP_NAME"
fi

# 移动文件
mkdir -p $SERVER_DIR/apps/
mv aipi-start/target/*.jar $SERVER_DIR/apps/
echo "Jar已经移动到${SERVER_DIR}/apps"
if [ $? -ne 0 ]; then
  exit 5
fi

# 启动
cd $SERVER_DIR/
sh $SERVER_DIR/startup-aipi.sh

